﻿using DDHDeepTunnel.WebServer.App_Start;
using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Description;

namespace DDHDeepTunnel.WebServer
{
    /// <summary>
    /// 添加自定义的头部输入框
    /// </summary>
    public class OptionalHeaderFilter : IOperationFilter
    {
        /// <summary>
        /// 是否包含头部
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="schemaRegistry"></param>
        /// <param name="apiDescription"></param>
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)

        {
            if (operation.parameters == null)
                operation.parameters = new List<Parameter>();

            var actionFilter = apiDescription.ActionDescriptor.GetCustomAttributes<SwaggerTokenFilter>().Any();
            var controllerFilter = apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<SwaggerTokenFilter>(true).Any();
            //添加头部信息输入框
            if (actionFilter || controllerFilter)
            {
                //非必填字段可以设置为required=false
                operation.parameters.Add(new Parameter { name = "introID", @in = "header", description = "introID", required = true, type = "string" });
            }

        }
    }
}